package com.efast.dmom.aps.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.dmom.aps.bean.CustomerPart;
import com.efast.dmom.aps.bean.ResultBean;
import com.efast.dmom.aps.entity.CustomerPartEntity;
import com.efast.dmom.aps.entity.base.IModificationFlags;
import com.efast.dmom.aps.service.CustomerPartService;

/**
 * To realize the customer and part relationship to add, delete, update and
 * check
 * 
 * @author WJ
 * @date 2020/2/24
 *
 */

@Controller
@RequestMapping("/customerPart")
public class CustomerPartController {

	@Autowired
	private CustomerPartService customerPartService;

	@RequestMapping(value = "/operateByCustomerPart")
	public @ResponseBody ResultBean operateByCustomerPart(@RequestBody CustomerPart customerPart) {
		
		CustomerPartEntity entity = customerPart.getInnerData();
		// Determine the new
		if (entity.getModificationFlag() == IModificationFlags.NEW) {
			return customerPartService.save(customerPart);
			// Judge update
		} else if (entity.getModificationFlag() == IModificationFlags.MODIFIED) {
			return customerPartService.update(customerPart);
			// Judge to delete
		} else if (entity.getModificationFlag() == IModificationFlags.DELETED) {
			return customerPartService.delete(customerPart);
		}
		return null;
	}

	// Get the relationship between customer and part through id
	@RequestMapping(value = "/getCustomerPartById/{id}")
	public @ResponseBody CustomerPart getCustomerPartById(@PathVariable long id) {
		return customerPartService.getById(id);
	}

	// Get the relationship between customer and part through name
	@RequestMapping(value = "/getCustomerPartByName/{name}")
	public @ResponseBody CustomerPart getCustomerPartByName(@PathVariable String name) {
		return customerPartService.getByName(name);
	}

	// Get the relationship between customer and part through customerId
	@RequestMapping(value = "/getCustomerPartByCustomerId/{customerId}")
	public @ResponseBody List<CustomerPart> getCustomerPartByCustomerId(@PathVariable long customerId) {
		return customerPartService.getCustomerPartByCustomerId(customerId);
	}

	// Get the relationship between customer and part through partId
	@RequestMapping(value = "/getCustomerPartByPartId/{partId}")
	public @ResponseBody List<CustomerPart> getCustomerPartByPartId(@PathVariable long partId) {
		return customerPartService.getCustomerPartByPartId(partId);
	}

}
